package priv.wangy.micro.common.utils;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * list相关工具
 * @author wangy
 * @since 2023-11-05
 */
public class ListUtil {
    /**
     * <p>
     * 将给定list拆分成多份
     * </p>
     *
     * @param list 目标list
     * @param n    每份的个数
     * @return java.util.List<java.util.List < T>> 拆分好的list
     * @author 田伟
     * @since 2022-09-16 9:38
     */
    public static <T> List<List<T>> getSplitList(List<T> list, int n) {
        List<List<T>> groupList = Lists.newArrayList();
        if (list == null || n <= 0) {
            return groupList;
        }
        List<T> newList = Lists.newArrayList(list);
        int len = newList.size();
        int num = len % n == 0 ? len / n : (len / n + 1);
        for (int i = 0; i < num; i++) {
            groupList.add(list.subList(i * n, Math.min((i + 1) * n, len)));
        }
        return groupList;
    }

    /**
     * map 转化成二维数组
     * @author wangy
     * @since 10:30 2024/7/15
     * @param map
     * @return java.util.List<java.util.List < java.lang.Integer>>
     **/
    public static List<List<Integer>> mapToList(Map<Integer, Long> map) {
        List<List<Integer>> list = new ArrayList<>();
        for (Map.Entry<Integer, Long> entry : map.entrySet()) {
            List<Integer> innerList = new ArrayList<>();
            innerList.add(entry.getKey());
            innerList.add(entry.getValue().intValue());
            list.add(innerList);
        }
        return list;
    }
}
